package com.bbgwap.controller;

import com.alibaba.fastjson.JSON;
import com.bbgkh.model.BO.SaleInfoBO;
import com.bbgkh.model.BaseInfo;
import com.bbgkh.model.DTO.SaleInfoDTO;
import com.bbgkh.model.PO.CustomerPO;
import com.bbgkh.model.PO.SaleInfoPO;
import com.bbgkh.service.ISaleService;
import com.bbgkh.service.IUserService;
import com.bbgkh.utils.BBLogger;
import com.bbgkh.utils.StringUtils;
import com.bbgkh.utils.UserUtils;
import com.bbgwap.model.WeChatInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.bbgwap.base.APPCONST.*;

/**
 * Created by lixiang01 on 2/10/2017.
 */
@RestController
@CrossOrigin
public class SaleController extends BaseController{

    @Autowired
    private ISaleService saleService;

    @Autowired
    private IUserService userService;

    @PostMapping(value = "sale/newSaleInfo")
    public String newSaleInfo(@RequestBody SaleInfoDTO saleInfoDTO, HttpServletRequest request, HttpServletResponse response){
        BaseInfo baseInfo = null;
        SaleInfoBO saleInfoBO = new SaleInfoBO();
        BeanUtils.copyProperties(saleInfoDTO,saleInfoBO);
        String uid = UserUtils.getUid(request);
        saleInfoBO.setUid(uid);

        try {
            baseInfo= saleService.insert(saleInfoBO);
        } catch (Exception e) {
            logger.error("插入销售数据失败",e);

            baseInfo= new BaseInfo("100","插入销售数据失败");

        }

        return JSON.toJSONString(baseInfo);
    }
    @GetMapping(value = "sale/getSaleById/{id}")
    public String getSaleInfoById(@PathVariable String id, HttpServletRequest request, HttpServletResponse response){
        BaseInfo baseInfo = null;


        baseInfo = saleService.selectById(id);


        return JSON.toJSONString(baseInfo);
    }
    @GetMapping(value = "sale/deleteSaleById/{id}")
    public String deleteSaleById(@PathVariable String id, HttpServletRequest request, HttpServletResponse response){
        BaseInfo baseInfo = null;

        baseInfo = saleService.deleteById(id);


        return JSON.toJSONString(baseInfo);
    }

//    @PostMapping(value = "sale/updateSale")
//    public String updateSale(@RequestBody SaleInfoPO saleInfoPO, HttpServletRequest request, HttpServletResponse response){
//        BaseInfo baseInfo = null;
//
//
//        String uid = UserUtils.getUid(request);
//        saleInfoPO.setUid(uid);
//
//        int a  = saleService.update(saleInfoPO);
//
//        baseInfo = new BaseInfo("0","更新完成");
//
//        return JSON.toJSONString(baseInfo);
//    }


}
